package com.algorithm.ch1.cjm.data;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author jieming
 */
public class Map {

    public static void main(String[] args) {

        Map.hashMap();
    }

    /**
     * key 不能为空，value不能为空
     * 线程安全
     */
    private static void hashTable(){

        Hashtable<Object, Object> hashTable = new Hashtable<>();

        hashTable.put(null, "11");
        hashTable.put(null, null);
        hashTable.put("11", null);

        System.out.println(hashTable);
    }

    /**
     * key 不能为空，value不能为空
     * 锁分段技术(JDK8:CAS
     */
    private static void concurrentHashMap(){

        ConcurrentHashMap<Object, Object> concurrentHashMap = new ConcurrentHashMap<>();

//        concurrentHashMap.put("11", null);
//        concurrentHashMap.put(null, null);
        concurrentHashMap.put(null, 111);

        System.out.println(concurrentHashMap);
    }

    /**
     * key不允许为空，value允许为空
     * 线程不安全
     */
    private static void treeMap(){

        TreeMap<Object, Object> treeMap = new TreeMap<>();
        treeMap.put("11", null);
        treeMap.put("22", null);

        System.out.println(treeMap.toString());
    }

    /**
     * 可以允许为空，value允许为空
     * 线程不安全
     */
    private static void hashMap(){

        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put(null, null);

        System.out.println(hashMap.toString());
    }

}
